﻿// =================================================================== 
// 模块说明
//====================================================================
// 创建时间：2018-12-15 16:30:13
// 负责人：liucx
// ===================================================================
using System;
using System.Data;
using System.IO;
using System.Configuration;
using log4net.Appender;
using log4net.Core;
using log4net.Layout;
using log4net.Layout.Pattern;
using OF.Component.Model;

namespace LogCenter.LogCustom
{
    #region 入库sql和参数
    public class ActionLogAdoAppender : OperationLogInfoCustomAdoNetAppender
    {
        public ActionLogAdoAppender()
        {
            //System.Data.SqlClient
            Type t = typeof(System.Data.SqlClient.SqlConnection);
            string s = t.Assembly.FullName.ToString();
            //数据连接类型
            this.ConnectionType = $"System.Data.SqlClient.SqlConnection, {s}";
            base.UseTransactions = false;
            this.CommandType = CommandType.Text;
            //sql语句
            this.CommandText = @"INSERT INTO OperationLog			
			(			
			[OpSystem],
			[OpModule],
			[OpBusiness],
			[OpMethod],
			[OPMethodParams],
			[OpType],
			[OpKeyField],
			[ServerIP],
			[ServerSystem],
			[SN],
			[OldData],
			[NewData],
			[OpContent],
			[CreatedDate],
			[OpUserID],
			[OpUserName],
			[ServerUrl]
			)
			 VALUES
			(			
			@OpSystem,
			@OpModule,
			@OpBusiness,
			@OpMethod,
			@OPMethodParams,
			@OpType,
			@OpKeyField,
			@ServerIP,
			@ServerSystem,
			@SN,
			@OldData,
			@NewData,
			@OpContent,
			@CreatedDate,
			@OpUserID,
			@OpUserName,
			@ServerUrl
			)
			 SET @OperationLogID=@@IDENTITY
			";
            string parameterName = "";
            parameterName = "OperationLogID";
            this.AddParameter(new AdoNetAppenderParameter() { ParameterName = "@" + parameterName, DbType = DbType.Int64, Layout = new Layout2RawLayoutAdapter(new ActionLogLayout("%" + parameterName)) });
            parameterName = "OpSystem";
            this.AddParameter(new AdoNetAppenderParameter() { ParameterName = "@" + parameterName, DbType = DbType.String, Size = 50, Layout = new Layout2RawLayoutAdapter(new ActionLogLayout("%" + parameterName)) });
            parameterName = "OpModule";
            this.AddParameter(new AdoNetAppenderParameter() { ParameterName = "@" + parameterName, DbType = DbType.String, Size = 50, Layout = new Layout2RawLayoutAdapter(new ActionLogLayout("%" + parameterName)) });
            parameterName = "OpBusiness";
            this.AddParameter(new AdoNetAppenderParameter() { ParameterName = "@" + parameterName, DbType = DbType.String, Size = 50, Layout = new Layout2RawLayoutAdapter(new ActionLogLayout("%" + parameterName)) });
            parameterName = "OpMethod";
            this.AddParameter(new AdoNetAppenderParameter() { ParameterName = "@" + parameterName, DbType = DbType.String, Size = 50, Layout = new Layout2RawLayoutAdapter(new ActionLogLayout("%" + parameterName)) });
            parameterName = "OPMethodParams";
            this.AddParameter(new AdoNetAppenderParameter() { ParameterName = "@" + parameterName, DbType = DbType.String, Size = 500, Layout = new Layout2RawLayoutAdapter(new ActionLogLayout("%" + parameterName)) });
            parameterName = "OpType";
            this.AddParameter(new AdoNetAppenderParameter() { ParameterName = "@" + parameterName, DbType = DbType.String, Size = 50, Layout = new Layout2RawLayoutAdapter(new ActionLogLayout("%" + parameterName)) });
            parameterName = "OpKeyField";
            this.AddParameter(new AdoNetAppenderParameter() { ParameterName = "@" + parameterName, DbType = DbType.String, Size = 50, Layout = new Layout2RawLayoutAdapter(new ActionLogLayout("%" + parameterName)) });
            parameterName = "ServerIP";
            this.AddParameter(new AdoNetAppenderParameter() { ParameterName = "@" + parameterName, DbType = DbType.String, Size = 50, Layout = new Layout2RawLayoutAdapter(new ActionLogLayout("%" + parameterName)) });
            parameterName = "ServerSystem";
            this.AddParameter(new AdoNetAppenderParameter() { ParameterName = "@" + parameterName, DbType = DbType.String, Size = 50, Layout = new Layout2RawLayoutAdapter(new ActionLogLayout("%" + parameterName)) });
            parameterName = "SN";
            this.AddParameter(new AdoNetAppenderParameter() { ParameterName = "@" + parameterName, DbType = DbType.String, Size = 50, Layout = new Layout2RawLayoutAdapter(new ActionLogLayout("%" + parameterName)) });
            parameterName = "OldData";
            this.AddParameter(new AdoNetAppenderParameter() { ParameterName = "@" + parameterName, DbType = DbType.String, Size = 500, Layout = new Layout2RawLayoutAdapter(new ActionLogLayout("%" + parameterName)) });
            parameterName = "NewData";
            this.AddParameter(new AdoNetAppenderParameter() { ParameterName = "@" + parameterName, DbType = DbType.String, Size = 500, Layout = new Layout2RawLayoutAdapter(new ActionLogLayout("%" + parameterName)) });
            parameterName = "OpContent";
            this.AddParameter(new AdoNetAppenderParameter() { ParameterName = "@" + parameterName, DbType = DbType.String, Size = 1000, Layout = new Layout2RawLayoutAdapter(new ActionLogLayout("%" + parameterName)) });
            parameterName = "CreatedDate";
            this.AddParameter(new AdoNetAppenderParameter() { ParameterName = "@" + parameterName, DbType = DbType.DateTime, Layout = new Layout2RawLayoutAdapter(new ActionLogLayout("%" + parameterName)) });
            parameterName = "OpUserID";
            this.AddParameter(new AdoNetAppenderParameter() { ParameterName = "@" + parameterName, DbType = DbType.Int32, Layout = new Layout2RawLayoutAdapter(new ActionLogLayout("%" + parameterName)) });
            parameterName = "OpUserName";
            this.AddParameter(new AdoNetAppenderParameter() { ParameterName = "@" + parameterName, DbType = DbType.String, Size = 30, Layout = new Layout2RawLayoutAdapter(new ActionLogLayout("%" + parameterName)) });
            parameterName = "ServerUrl";
            this.AddParameter(new AdoNetAppenderParameter() { ParameterName = "@" + parameterName, DbType = DbType.String, Size = 50, Layout = new Layout2RawLayoutAdapter(new ActionLogLayout("%" + parameterName)) });

            base.ActivateOptions();
        }
    }
    #endregion

    #region 数据库连接
    public class OperationLogInfoCustomAdoNetAppender : AdoNetAppender
    {
        /// <summary>
        /// 数据库连接
        /// </summary>
        public new string ConnectionStringName
        {
            set
            {
                this.ConnectionString = ConfigurationManager.ConnectionStrings[value].ConnectionString;
            }
        }
    }
    #endregion

    #region 字段转换
    public class ActionLogLayout : PatternLayout
    {
        public ActionLogLayout(string pattern)
            : base(pattern)
        {

        }
        public override void ActivateOptions()
        {

            this.AddConverter("OperationLogID", typeof(ActionLogOperationLogIDConverter));
            this.AddConverter("OpSystem", typeof(ActionLogOpSystemConverter));
            this.AddConverter("OpModule", typeof(ActionLogOpModuleConverter));
            this.AddConverter("OpBusiness", typeof(ActionLogOpBusinessConverter));
            this.AddConverter("OpMethod", typeof(ActionLogOpMethodConverter));
            this.AddConverter("OPMethodParams", typeof(ActionLogOPMethodParamsConverter));
            this.AddConverter("OpType", typeof(ActionLogOpTypeConverter));
            this.AddConverter("OpKeyField", typeof(ActionLogOpKeyFieldConverter));
            this.AddConverter("ServerIP", typeof(ActionLogServerIPConverter));
            this.AddConverter("ServerSystem", typeof(ActionLogServerSystemConverter));
            this.AddConverter("SN", typeof(ActionLogSNConverter));
            this.AddConverter("OldData", typeof(ActionLogOldDataConverter));
            this.AddConverter("NewData", typeof(ActionLogNewDataConverter));
            this.AddConverter("OpContent", typeof(ActionLogOpContentConverter));
            this.AddConverter("CreatedDate", typeof(ActionLogCreatedDateConverter));
            this.AddConverter("OpUserID", typeof(ActionLogOpUserIDConverter));
            this.AddConverter("OpUserName", typeof(ActionLogOpUserNameConverter));
            this.AddConverter("ServerUrl", typeof(ActionLogServerUrlConverter));
            base.ActivateOptions();
        }
    }
     internal sealed class ActionLogOperationLogIDConverter : PatternLayoutConverter
    {
        override protected void Convert(TextWriter writer, LoggingEvent loggingEvent)
        {
            OperationLogInfo log = (OperationLogInfo)loggingEvent.MessageObject;
            if (log != null)
                writer.Write(log.OperationLogID);
        }
    }
    internal sealed class ActionLogOpSystemConverter : PatternLayoutConverter
    {
        override protected void Convert(TextWriter writer, LoggingEvent loggingEvent)
        {
            OperationLogInfo log = (OperationLogInfo)loggingEvent.MessageObject;
            if (log != null)
                writer.Write(log.OpSystem);
        }
    }
    internal sealed class ActionLogOpModuleConverter : PatternLayoutConverter
    {
        override protected void Convert(TextWriter writer, LoggingEvent loggingEvent)
        {
            OperationLogInfo log = (OperationLogInfo)loggingEvent.MessageObject;
            if (log != null)
                writer.Write(log.OpModule);
        }
    }
    internal sealed class ActionLogOpBusinessConverter : PatternLayoutConverter
    {
        override protected void Convert(TextWriter writer, LoggingEvent loggingEvent)
        {
            OperationLogInfo log = (OperationLogInfo)loggingEvent.MessageObject;
            if (log != null)
                writer.Write(log.OpBusiness);
        }
    }
    internal sealed class ActionLogOpMethodConverter : PatternLayoutConverter
    {
        override protected void Convert(TextWriter writer, LoggingEvent loggingEvent)
        {
            OperationLogInfo log = (OperationLogInfo)loggingEvent.MessageObject;
            if (log != null)
                writer.Write(log.OpMethod);
        }
    }
    internal sealed class ActionLogOPMethodParamsConverter : PatternLayoutConverter
    {
        override protected void Convert(TextWriter writer, LoggingEvent loggingEvent)
        {
            OperationLogInfo log = (OperationLogInfo)loggingEvent.MessageObject;
            if (log != null)
                writer.Write(log.OPMethodParams);
        }
    }
    internal sealed class ActionLogOpTypeConverter : PatternLayoutConverter
    {
        override protected void Convert(TextWriter writer, LoggingEvent loggingEvent)
        {
            OperationLogInfo log = (OperationLogInfo)loggingEvent.MessageObject;
            if (log != null)
                writer.Write(log.OpType);
        }
    }
    internal sealed class ActionLogOpKeyFieldConverter : PatternLayoutConverter
    {
        override protected void Convert(TextWriter writer, LoggingEvent loggingEvent)
        {
            OperationLogInfo log = (OperationLogInfo)loggingEvent.MessageObject;
            if (log != null)
                writer.Write(log.OpKeyField);
        }
    }
    internal sealed class ActionLogServerIPConverter : PatternLayoutConverter
    {
        override protected void Convert(TextWriter writer, LoggingEvent loggingEvent)
        {
            OperationLogInfo log = (OperationLogInfo)loggingEvent.MessageObject;
            if (log != null)
                writer.Write(log.ServerIP);
        }
    }
    internal sealed class ActionLogServerSystemConverter : PatternLayoutConverter
    {
        override protected void Convert(TextWriter writer, LoggingEvent loggingEvent)
        {
            OperationLogInfo log = (OperationLogInfo)loggingEvent.MessageObject;
            if (log != null)
                writer.Write(log.ServerSystem);
        }
    }
    internal sealed class ActionLogSNConverter : PatternLayoutConverter
    {
        override protected void Convert(TextWriter writer, LoggingEvent loggingEvent)
        {
            OperationLogInfo log = (OperationLogInfo)loggingEvent.MessageObject;
            if (log != null)
                writer.Write(log.SN);
        }
    }
    internal sealed class ActionLogOldDataConverter : PatternLayoutConverter
    {
        override protected void Convert(TextWriter writer, LoggingEvent loggingEvent)
        {
            OperationLogInfo log = (OperationLogInfo)loggingEvent.MessageObject;
            if (log != null)
                writer.Write(log.OldData);
        }
    }
    internal sealed class ActionLogNewDataConverter : PatternLayoutConverter
    {
        override protected void Convert(TextWriter writer, LoggingEvent loggingEvent)
        {
            OperationLogInfo log = (OperationLogInfo)loggingEvent.MessageObject;
            if (log != null)
                writer.Write(log.NewData);
        }
    }
    internal sealed class ActionLogOpContentConverter : PatternLayoutConverter
    {
        override protected void Convert(TextWriter writer, LoggingEvent loggingEvent)
        {
            OperationLogInfo log = (OperationLogInfo)loggingEvent.MessageObject;
            if (log != null)
                writer.Write(log.OpContent);
        }
    }
    internal sealed class ActionLogCreatedDateConverter : PatternLayoutConverter
    {
        override protected void Convert(TextWriter writer, LoggingEvent loggingEvent)
        {
            OperationLogInfo log = (OperationLogInfo)loggingEvent.MessageObject;
            if (log != null)
                writer.Write(log.CreatedDate);
        }
    }
    internal sealed class ActionLogOpUserIDConverter : PatternLayoutConverter
    {
        override protected void Convert(TextWriter writer, LoggingEvent loggingEvent)
        {
            OperationLogInfo log = (OperationLogInfo)loggingEvent.MessageObject;
            if (log != null)
                writer.Write(log.OpUserID);
        }
    }
    internal sealed class ActionLogOpUserNameConverter : PatternLayoutConverter
    {
        override protected void Convert(TextWriter writer, LoggingEvent loggingEvent)
        {
            OperationLogInfo log = (OperationLogInfo)loggingEvent.MessageObject;
            if (log != null)
                writer.Write(log.OpUserName);
        }
    }
    internal sealed class ActionLogServerUrlConverter : PatternLayoutConverter
    {
        override protected void Convert(TextWriter writer, LoggingEvent loggingEvent)
        {
            OperationLogInfo log = (OperationLogInfo)loggingEvent.MessageObject;
            if (log != null)
                writer.Write(log.ServerUrl);
        }
    }
    #endregion

}